<?php


class AddressManagement extends CI_Controller {

	function index()
	{
		$item[0]='地址管理';
		
		$this->load->model('mycenter_model');
		$userId = $this->session->userdata('user_id');
		//取user地址信息
		if($userId!=0)
		{
			$this->load->model('deliveraddress_model');
			$deliverAddressArray = $this->deliveraddress_model->getAddressByUserid($userId);
		}else {
			$deliverAddressArray = array();
		}
		
		$data['deliverAddressArray'] = $deliverAddressArray;
		$url = base_url()."myCenterFolder/addressManagement";
		$data['url'] = $url;
		$data['item']=$item;		
		$data['left_tab'] = 'address_management';
		$this->load->view('myCenter/myCenter_home',$data);
	}
	
	function addAddress()
	{
		$this->load->library('form_validation');
		//form验证，为了实现Re-populating，需要验证，trim就可以了
		$this->form_validation->set_rules('radio_address', '地址', 'trim');
		$this->form_validation->set_rules('user_sex', '性别', 'trim');
		$this->form_validation->set_rules('deliverMemo', '备注信息', 'trim');
		
		$this->form_validation->set_message('required', '请正确填写 %s');
		
		$this->form_validation->set_rules('deliverAddress', '地址', 'required');
		$this->form_validation->set_rules('deliverName', '姓名', 'required');
		$this->form_validation->set_rules('deliverPhone', '电话', 'required');
		if ($this->form_validation->run() == FALSE)
		{
			$this->index();
			return;
		}
		$data['fullAddress'] = $_POST['deliverAddress'];	
		$data['name'] = $_POST['deliverName'];
        $data['phone']= $_POST['deliverPhone'];
        //$data['phoneOptional']= $_POST['deliverAddress'];
		$data['isDefault'] = true; 
		$userId = $this->session->userdata('user_id');
        $data['userId']= $userId;
        $this->load->model('deliveraddress_model');
        $this->deliveraddress_model->insert_entry($data);
        $this->index();
	}
	
	function deleteAddress($addressId){
		$userId = $this->session->userdata('user_id');
        $this->load->model('deliveraddress_model');
        $this->deliveraddress_model->delete_address_by_id_and_user_id($addressId, $userId);
        $this->index();
	}
	
	function updateAddress(){
		
	}
}